//
//  NSString+CycleFileSize.m
//  CycleControlFunction
//
//  Created by ddy on 2018/8/7.
//  Copyright © 2018年 cycle. All rights reserved.
//

#import "NSString+CycleFileSize.h"
#import <UIKit/UIKit.h>

static CGFloat const BFine = 1000.0;//苹果电脑按1000.0算
//static CGFloat const BFine = 1024.0;

@implementation NSString (CycleFileSize)

#pragma mark --------  1. MB单位
/**计算某个文件所占内存大小(方法一)，单位 MB*/
- (float)cycle_fileSize_MB1{
    //总大小
    float size = 0;
    //文件管理者
    NSFileManager *manager = [NSFileManager defaultManager];
    //是否是文件夹
    BOOL isDirectory = NO;
    
    BOOL exits = [manager fileExistsAtPath:self isDirectory:&isDirectory];
    //文件夹不存在
    if (!exits)return size;
    
    //是文件夹
    if (isDirectory) {
        
        NSDirectoryEnumerator *enumators = [manager enumeratorAtPath:self];
        for (NSString *path in enumators) {
            //全路径
            NSString *fullPath = [self stringByAppendingPathComponent:path];
            //得到大小
            size += [manager attributesOfItemAtPath:fullPath error:nil].fileSize;
        }
    }else{
        //是文件，如：logo.png
        size = [manager attributesOfItemAtPath:self error:nil].fileSize;
    }
    return size/BFine/BFine;
}




/**计算某个文件所占内存大小  (方法二)，单位 MB*/
- (float)cycle_fileSize_MB2{
    //总大小
    float size = 0.0;
    //文件管理者
    NSFileManager *manager = [NSFileManager defaultManager];
    //文件属性
    NSDictionary *attrs = [manager attributesOfItemAtPath:self error:nil];
    //判断是否为文件夹
    if ([attrs.fileType isEqualToString:NSFileTypeDirectory]) {
        NSDirectoryEnumerator *subPaths = [manager enumeratorAtPath:self];
        for (NSString *subPath in subPaths) {
            //全路径
            NSString *fullPath = [self stringByAppendingPathComponent:subPath];
            //累加文件大小
            size += [manager attributesOfItemAtPath:fullPath error:nil].fileSize;
        }
    }else{
        //文件
        size = attrs.fileSize;
    }
    
    
    return size/BFine/BFine;
}

#pragma mark --------  2. B单位

/**计算某个文件所占内存大小(方法一)，单位 B*/
- (float)cycle_fileSize_B1{
    //总大小
    float size = 0;
    //文件管理者
    NSFileManager *manager = [NSFileManager defaultManager];
    //是否是文件夹
    BOOL isDirectory = NO;
    
    BOOL exits = [manager fileExistsAtPath:self isDirectory:&isDirectory];
    //文件夹不存在
    if (!exits)return size;
    
    //是文件夹
    if (isDirectory) {
        
        NSDirectoryEnumerator *enumator = [manager enumeratorAtPath:self];
        for (NSString *path in enumator) {
            //全路径
            NSString *fullPath = [self stringByAppendingPathComponent:path];
            //得到大小
            size += [manager attributesOfItemAtPath:fullPath error:nil].fileSize;
        }
    }else{
        //是文件，如：logo.png
        size = [manager attributesOfItemAtPath:self error:nil].fileSize;
    }
    return size;
}




/**计算某个文件所占内存大小  (方法二)，单位 MB*/
- (float)cycle_fileSize_B2{
    //总大小
    float size = 0.0;
    //文件管理者
    NSFileManager *manager = [NSFileManager defaultManager];
    //文件属性
    NSDictionary *attrs = [manager attributesOfItemAtPath:self error:nil];
    //判断是否为文件夹
    if ([attrs.fileType isEqualToString:NSFileTypeDirectory]) {
        NSDirectoryEnumerator *subPaths = [manager enumeratorAtPath:self];
        for (NSString *subPath in subPaths) {
            //全路径
            NSString *fullPath = [self stringByAppendingPathComponent:subPath];
            //累加文件大小
            size += [manager attributesOfItemAtPath:fullPath error:nil].fileSize;
        }
    }else{
        //文件
        size = attrs.fileSize;
    }
    
    
    return size;
}



///------------------------------------ 读取，保存数据到本地 ----------------------------------
id type;
/** 读取文件city.plist,data.json
 *  1.传入json文件或者plist文件返回字典，如（red.json）
 */
+ (id)cycle_getDataFromLocalFileName:(NSString *)fileName{
    NSString *path = [[NSBundle mainBundle]pathForResource:fileName ofType:nil];
    NSError *error = nil;
    id data = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:path] options:NSJSONReadingMutableContainers | NSJSONReadingAllowFragments error:&error];
    if(error != nil || data == nil){
        return nil;
    } else {
        return data;
    }
}
/**
 * 2.1 保存后台数据到本地
  eg:NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"data.json"];
 */
+ (void)cycle_saveLocalData:(id)data filePath:(NSString *)filePath {
    void (^block)(BOOL result) = ^(BOOL result){
        if (result) {
            NSLog(@"写入成功");
        } else {
            NSLog(@"写入失败");
        }
    };
    //判断类型
    if ([data isKindOfClass:[NSDictionary class]]) {
        NSLog(@"传入了字典类型的数据");
        //执行储存
        NSDictionary *dicData = data;
        BOOL result = [dicData writeToFile:filePath atomically:YES];
        //打印
        block(result);
        type = dicData;
    } else if([data isKindOfClass:[NSArray class]]){
        NSLog(@"传入了数组类型的数据");
        //执行储存
        NSArray *arrayData = data;
        BOOL result = [arrayData writeToFile:filePath atomically:YES];
        //打印
        block(result);
        type = arrayData;
    } else if([data isKindOfClass:[NSString class]]){
        //执行储存
        NSString *stringData = data;
        BOOL result = [stringData writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
        //打印
        block(result);
        type = stringData;
    } else {
        NSLog(@"暂不支持的储存类型");
    }
   
    
}
/**
 * 2.2 将保存到本地的后台数据取出来
 */
+ (id)cycle_getLocalDataWithFilePath:(NSString *)filePath {
    //1.如果储存了字典类型
    if ([type isKindOfClass:[NSDictionary class]]) {
      return [NSDictionary dictionaryWithContentsOfFile:filePath];
    } else if ([type isKindOfClass:[NSArray class]]) {
        return [NSArray arrayWithContentsOfFile:filePath];
    } else if ([type isKindOfClass:[NSString class]]) {
        return [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    } else {
        //暂不支持的数据储存
        return nil;
    }
}
@end
